package com.itheima.d2_buffered_stream;

import java.io.*;

/**
 * 目标：掌握字节缓冲流的作用。
 */
public class Test1BufferedInputStream {
    public static void main(String[] args) {
        File file = new File("day09file-io-app/img1/1.jpg");
        File dest = new File("day09file-io-app/img2/1.jpg");
        //copyFile1(file,dest);
        copyFile2(file,dest);

    }
    //将源文件src拷贝到目标文件dest中
    public static void copyFile1(File file,File dest){
        //使用基本流完成文件的拷贝 FileInputStream FileOutStream
        long t1 = System.currentTimeMillis();
        try(FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(dest);){

            int b;
            while((b = fis.read()) !=-1){
                fos.write(b);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        long t2 = System.currentTimeMillis();

        System.out.println(t2-t1);

    }
    public static void copyFile2(File file,File dest){
        //使用基本流完成文件的拷贝 FileInputStream FileOutStream
        long t1 = System.currentTimeMillis();
        try(FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream(dest);
             BufferedInputStream bis = new BufferedInputStream(fis);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
        ){
            int len;
            byte[] bytes = new byte[1024];
            while((len = bis.read(bytes)) !=-1){
                bos.write(bytes,0,len);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        long t2 = System.currentTimeMillis();

        System.out.println((t2-t1)/1000.0);
    }
}
